package com.lznAdmin.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lznAdmin.base.common.api.service.CommonAPI;
import com.lznAdmin.base.common.constant.CacheConst;
import com.lznAdmin.base.common.util.RedisUtil;
import com.lznAdmin.base.common.vo.DictVO;
import com.lznAdmin.system.entity.SysDictMain;
import com.lznAdmin.system.mapper.SysDictMainMapper;
import com.lznAdmin.system.service.SysDictMainService;
import com.lznAdmin.system.service.SysDictSubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * @program: lznAdmin-pro
 * @description:
 * @author: lzn
 * @create: 2022-02-18 16:52
 **/
@Slf4j
@Service
public class SystemApiImpl implements CommonAPI {
    @Autowired
    private SysDictMainService sysDictMainService;
    @Autowired
    private SysDictMainMapper sysDictMainMapper;
    @Autowired
    private SysDictSubService sysDictSubService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Cacheable(value = CacheConst.SYS_DICT_CACHE, key = "#dictCode", unless = "#result == null ")
    public List<DictVO> translateDict(String dictCode) {
        List<DictVO> mainAndSubListByCode = sysDictMainService.getMainAndSubListByCode(dictCode);
        log.info("mainAndSubListByCode={}", mainAndSubListByCode);
        if (mainAndSubListByCode.size() <= 0) {
            return null;
        }
        return mainAndSubListByCode;
    }

    @Override
    @Cacheable(value = CacheConst.SYS_DICT_TABLE_CACHE, key = "#dictCode", condition = "#isCache == 1", unless = "#result == null ")
    public List<LinkedHashMap<String, Object>> translateTableDict(String dictCode, int isCache) {
        QueryWrapper<SysDictMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", dictCode);
        queryWrapper.select("sql_text");
        SysDictMain sysDictMain = sysDictMainMapper.selectOne(queryWrapper);
        //去除开头的select字符串
        String sql_text = sysDictMain.getSqlText().replaceFirst("(?i)select", "");
        List<LinkedHashMap<String, Object>> selectTableDictList = sysDictMainMapper.selectTableDictList(sql_text);
        if (selectTableDictList.size() <= 0) {
            return null;
        }
        return selectTableDictList;
    }

    @Override
    public void ClearDictCache(String dictCode) {
        // 如果dictCode为""或null，则清除所有字典
        if (StrUtil.isEmpty(dictCode)) {
            Set<String> keys = redisTemplate.keys(CacheConst.BASE_CACHE + ":*");
            if (keys.size() > 0) {
                keys.forEach((String key) -> {
                    redisUtil.del(key);
                });
            }
        } else {
            redisUtil.del(CacheConst.SYS_DICT_CACHE + "::" + dictCode);
            redisUtil.del(CacheConst.SYS_DICT_TABLE_CACHE + "::" + dictCode);
        }
    }
}
